package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.ArticleBean;
import com.hxkcloud.nurse.utils.ImageOptionUtils;
import com.hxkcloud.nurse.utils.ImageUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by ray_lei on 2016/7/6.
 */
public class ArticalListAdapter extends RecyclerView.Adapter<ViewHolder> {


    private Context mContext;
    private List<ArticleBean> datas;
    private RecyclerView recyclerview;
    private LayoutInflater mInflater;

    private ResultSelectedListener listener;
    protected ImageLoader imageLoader;
    private int screenWidth;

    public ArticalListAdapter(Context mContext, List<ArticleBean> datas,
                              RecyclerView recyclerview, ResultSelectedListener listener, int width) {
        this.mContext = mContext;
        this.datas = datas;
        this.recyclerview = recyclerview;
        this.listener = listener;
        mInflater = LayoutInflater.from(mContext);
        this.screenWidth = width;
        imageLoader = ImageLoader.getInstance();
    }

    public void setData(List<ArticleBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return datas.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // TODO Auto-generated method stub


        Holder mHolder = (Holder) holder;
        if (datas.get(position).isSelect()) {
            mHolder.selectBtn.setBackground(mContext.getResources().getDrawable(R.mipmap.btn_multiselect_pre));
        } else {
            mHolder.selectBtn.setBackground(mContext.getResources().getDrawable(R.mipmap.btn_multiselect));
        }
        LinearLayout.LayoutParams lp_head = new LinearLayout.LayoutParams(screenWidth / 6, screenWidth / 6);
        lp_head.setMargins(20, 0, 0, 0);
        mHolder.icon.setLayoutParams(lp_head);
        imageLoader.displayImage(datas.get(position).getImg(), mHolder.icon, ImageOptionUtils.getInstance().getArticlePicSmallOptions(ImageUtil.dip2px(5, mContext)));
        mHolder.titleTV.setText(datas.get(position).getName());
        mHolder.descriptionTV.setText(datas.get(position).getIntro());
        mHolder.selectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (datas.get(position).isSelect()) {
                    listener.onSelectedArtical("no_select", position);
                } else {
                    listener.onSelectedArtical("select", position);
                }

            }
        });
//        mHolder.ageTV.setText("");
//        mHolder.sexTV.setText("");
//        mHolder.statusTV.setText("");
//        mHolder.iconCI.setImageResource();
//        mHolder.descrptionTV.setText("");


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.item_artical_layout, null);
        ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /*
     * Holder
     */
    public class Holder extends ViewHolder {

        private LinearLayout layout;
        private Button selectBtn;
        private ImageView icon;
        private TextView titleTV;
        private TextView descriptionTV;

        public Holder(View view) {
            super(view);
            layout = (LinearLayout) view.findViewById(R.id.artical_layout_whole);
            selectBtn = (Button) view.findViewById(R.id.artical_btn_select);
            icon = (ImageView) view.findViewById(R.id.artical_iv_icon);
            titleTV = (TextView) view.findViewById(R.id.artical_tv_desciption1);
            descriptionTV = (TextView) view.findViewById(R.id.artical_tv_desciption);
        }
    }

    public interface ResultSelectedListener {
        void onSelectedArtical(String type, int position);
    }


}
